package com.twu.biblioteca;

import java.util.ArrayList;

public class BibliotecaApp {

    private BookGetter bookGetter;
    private Input input;
    private Output output;
    private ArrayList<String> options;
    private MovieGetter movieGetter;
    private UserCollection userCollection;
    private User loggedInUser = null;

    public User getLoggedInUser(){
        return loggedInUser;
    }

    public void showMenuOptions() {
        output.printMenuOptions(options);
    }


    private enum MenuOptions {
        SHOW_BOOKS, RESERVE_BOOKS, CHECK_MEMBERSHIP, SHOW_MOVIES, PROMPT_USERINFO
    }


    public BibliotecaApp(BookGetter bookGetter, Input input, Output output, MovieGetter movieGetter, UserCollection userCollection)
    {   this.options = new ArrayList<String>();
        this.options.add("See List of Books");
        this.options.add("Reserve Book");
        this.options.add("Check Membership");
        this.options.add("See List of Movies");
        this.options.add("Login");
        this.bookGetter= bookGetter;
        this.input = input;
        this.output = output;
        this.movieGetter = movieGetter;
        this.userCollection = userCollection;
    }

    public void processMenuOption(int option)
    {
        if(option < 0 || option >= options.size())
        {
            output.printValidOptionPrompt();
            return;
        }
        MenuOptions bookGetterOption = MenuOptions.values()[option];
        switch(bookGetterOption){
            case SHOW_BOOKS:
                bookGetter.showBooks();
                break;
            case RESERVE_BOOKS:
                bookGetter.reserveBook();
                break;
            case CHECK_MEMBERSHIP:
                bookGetter.checkMembership();
                break;
            case SHOW_MOVIES:
                movieGetter.showMovies();
                break;
            case PROMPT_USERINFO:
                output.showUsernamePrompt();
                String userNumber = input.getUserNumber();
                output.showPasswordPrompt();
                String password = input.getUserPassword();
                boolean loginAccepted = userCollection.validateUser(userNumber, password);

                if(loginAccepted){
                    output.printMenuOptions(options);
                    processMenuOption(input.getUserInput());
                    loggedInUser = userCollection.getUserFromUsername(userNumber);
                } else {
                    output.printInvalidLoginMessage();
                }
                break;
        }
    }

    public void start() {
        output.welcome();
        this.showMenuOptions();
        this.processMenuOption(input.getUserInput());
    }
}
